﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Script.Serialization;

namespace yao.Common
{
    public static class CookieHelper
    {
        private const string _key = "bokpriky";

        #region 移除cookie
        /// <summary>
        /// 移除cookie
        /// </summary>
        /// <param name="response">当前的http响应</param>
        /// <param name="name">要移除的cookie名</param>
        public static void RemoveCookie(this HttpResponse response, string name)
        {
            response.AppendCookie(CreateCookie(name, string.Empty, DateTime.Now.AddDays(-1)));
        }

        /// <summary>
        /// 移除cookie
        /// </summary>
        /// <param name="response">当前的http响应</param>
        /// <param name="name">要移除的cookie名</param>
        public static void RemoveCookie(this HttpResponseBase response, string name)
        {
            response.AppendCookie(CreateCookie(name, string.Empty, DateTime.Now.AddDays(-1)));
        }
        #endregion

        #region 设置cookie值
        /// <summary>
        /// 设置cookie值
        /// </summary>
        /// <param name="response">当前http响应</param>
        /// <param name="name">cookie名</param>
        /// <param name="value">cookie值</param>
        /// <param name="expires">过期时间</param>
        public static void SetCookie(this HttpResponse response, string name, string value, DateTime? expires)
        {
            response.AppendCookie(CreateCookie(name, value, expires));
        }
        /// <summary>
        /// 设置cookie值
        /// </summary>
        /// <param name="response">当前http响应</param>
        /// <param name="name">cookie名</param>
        /// <param name="value">cookie值</param>
        public static void SetCookie(this HttpResponse response, string name, string value)
        {
            response.AppendCookie(CreateCookie(name, value, null));
        }
        /// <summary>
        /// 设置cookie值
        /// </summary>
        /// <param name="response">当前http响应</param>
        /// <param name="name">cookie名</param>
        /// <param name="value">cookie值</param>
        public static void SetCookie(this HttpResponse response, string name, object value)
        {
            response.AppendCookie(CreateCookie(name, value, null));
        }

        /// <summary>
        /// 设置cookie值
        /// </summary>
        /// <param name="response">当前http响应</param>
        /// <param name="name">cookie名</param>
        /// <param name="value">cookie值</param>
        /// <param name="expires">过期时间</param>
        public static void SetCookie(this HttpResponse response, string name, object value, DateTime? expires)
        {
            response.AppendCookie(CreateCookie(name, value, expires));
        }
        /// <summary>
        /// 设置cookie值
        /// </summary>
        /// <param name="response">当前http响应</param>
        /// <param name="name">cookie名</param>
        /// <param name="value">cookie值</param>
        /// <param name="expires">过期时间</param>
        public static void SetCookie(this HttpResponseBase response, string name, string value, DateTime? expires)
        {
            response.AppendCookie(CreateCookie(name, value, expires));
        }
        /// <summary>
        /// 设置cookie值
        /// </summary>
        /// <param name="response">当前http响应</param>
        /// <param name="name">cookie名</param>
        /// <param name="value">cookie值</param>
        public static void SetCookie(this HttpResponseBase response, string name, string value)
        {
            response.AppendCookie(CreateCookie(name, value, null));
        }
        /// <summary>
        /// 设置cookie值
        /// </summary>
        /// <param name="response">当前http响应</param>
        /// <param name="name">cookie名</param>
        /// <param name="value">cookie值</param>
        public static void SetCookie(this HttpResponseBase response, string name, object value)
        {
            response.AppendCookie(CreateCookie(name, value, null));
        }
        /// <summary>
        /// 设置cookie值
        /// </summary>
        /// <param name="response">当前http响应</param>
        /// <param name="name">cookie名</param>
        /// <param name="value">cookie值</param>
        /// <param name="expires">过期时间</param>
        public static void SetCookie(this HttpResponseBase response, string name, object value, DateTime? expires)
        {
            response.AppendCookie(CreateCookie(name, value, expires));
        }

        /// <summary>
        /// 创建HttpCookie对象
        /// </summary>
        /// <param name="name">cookie名</param>
        /// <param name="value">cookie值</param>
        /// <param name="expires">过期时间，如果为null，则浏览器关闭后失效</param>
        /// <returns></returns>
        public static HttpCookie CreateCookie(string name, object value, DateTime? expires = null)
        {
            HttpCookie cookie = new HttpCookie(name);
            if (value is string)
            {
                cookie.Value = Encryption.Encrypt((string)value, _key);
            }
            else
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                cookie.Value = Encryption.Encrypt(js.Serialize(value), _key);
            }
            if (expires.HasValue)
            {
                cookie.Expires = expires.Value;
            }
            return cookie;
        }

        #endregion

        #region 获取cookie值
        /// <summary>
        /// 获取cookie值，如果cookie不存在，返回string.Empty
        /// </summary>
        /// <param name="request">当前http请求</param>
        /// <param name="name">cookie名</param>
        /// <returns></returns>
        public static string GetCookieValue(this HttpRequest request, string name)
        {
            return GetCookieValue(request.Cookies[name]);
        }
        public static string GetCookieString(this HttpRequest request, string name)
        {
            return GetCookieString(request.Cookies[name]);
        }
        /// <summary>
        /// 获取cookie值，如果cookie不存在，返回string.Empty
        /// </summary>
        /// <param name="request">当前http请求</param>
        /// <param name="name">cookie名</param>
        /// <returns></returns>
        public static string GetCookieValue(this HttpRequestBase request, string name)
        {
            return GetCookieValue(request.Cookies[name]);
        }
        public static string GetCookieString(this HttpRequestBase request, string name)
        {
            return GetCookieString(request.Cookies[name]);
        }

        /// <summary>
        /// 获取cookie值，如果cookie不存在，返回null
        /// </summary>
        /// <typeparam name="T">要返回的类型</typeparam>
        /// <param name="request">当前http请求</param>
        /// <param name="name">cookie名</param>
        /// <returns></returns>
        public static T GetCookieValue<T>(this HttpRequest request, string name) where T : class
        {
            return GetCookieValue<T>(request.Cookies[name]);
        }
        /// <summary>
        /// 获取cookie值，如果cookie不存在，返回null
        /// </summary>
        /// <typeparam name="T">要返回的类型</typeparam>
        /// <param name="request">当前http请求</param>
        /// <param name="name">cookie名</param>
        /// <returns></returns>
        public static T GetCookieValue<T>(this HttpRequestBase request, string name) where T : class
        {
            return GetCookieValue<T>(request.Cookies[name]);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cookie"></param>
        /// <returns></returns>
        private static T GetCookieValue<T>(HttpCookie cookie) where T : class
        {
            if (cookie == null || string.IsNullOrEmpty(cookie.Value))
            {
                return default(T);
            }

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<T>(Encryption.Decrypt(cookie.Value, _key));
        }

        /// <summary>
        /// 获取cookie对象的值，cookie为空则返回string.Empty
        /// </summary>
        /// <param name="cookie"></param>
        /// <returns></returns>
        private static string GetCookieValue(HttpCookie cookie)
        {
            if (cookie == null)
            {
                return string.Empty;
            }
            return Encryption.Decrypt(cookie.Value, _key);
        }
        private static string GetCookieString(HttpCookie cookie)
        {
            if (cookie == null)
            {
                return string.Empty;
            }
            return cookie.Value;
        }
        #endregion
    }
}
